package it.txt.access.capability.finder.util;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.client.utils.URIBuilder;

/**
 *
 * @author seccia
 * @author Salvatore Piccione (TXT e-solutions SpA - salvatore.piccione AT network.txtgroup.com)
 */
public class URIResourceIDSections {

    private String scheme;
    private String host;
    private int port = UNDEFINED_PORT;
    private String service;
    private String namespace;
    private String pattern;
    private static final String COLON = ":";
    private static final int UNDEFINED_PORT = -1;
    private static final String NAMESPACE_QUERY_PARAM = "Namespace";
    private static final String PATTERN_QUERY_PARAM = "Pattern";

    public String getAuthority() {
        if (port > UNDEFINED_PORT)
            return host + COLON + port;
        else
            return host;
    }

    public void setAuthority(String authority) {
        int colonIndex = authority.indexOf(COLON);
        if (colonIndex == -1)
            this.host = authority;
        else {
            this.host = authority.substring(0, colonIndex);
            this.port = Integer.parseInt(authority.substring(colonIndex + COLON.length()));
        }
    }

    public String getNamespace() {
        return namespace;
    }

    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public String getScheme() {
        return scheme;
    }

    public void setScheme(String scheme) {
        this.scheme = scheme;
    }

    public String getService() {
        return service;
    }

    public void setService(String service) {
        this.service = service;
    }

    @Override
    public String toString(){
        try {
            return toURI().toString();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }
    
    public URI toURI() throws UnsupportedEncodingException, URISyntaxException {
        URIBuilder uriBuilder = new URIBuilder();
        uriBuilder.addParameter(NAMESPACE_QUERY_PARAM, this.namespace);
        uriBuilder.addParameter(PATTERN_QUERY_PARAM, this.pattern);
        uriBuilder.setHost(this.host);
        if (port > UNDEFINED_PORT)
            uriBuilder.setPort(port);
        uriBuilder.setScheme(this.scheme);
        uriBuilder.setPath((!this.service.startsWith("/") ? "/" : "") + this.service);
        return uriBuilder.build();
//        OLD
//        return new URI(scheme, authority, (!this.service.startsWith("/") ? "/" : "") + this.service, 
//                NAMESPACE_QUERY_PARAM + "=" + URLEncoder.encode(this.namespace,"UTF-8") + "&" + 
//                PATTERN_QUERY_PARAM + "=" + URLEncoder.encode(this.pattern,"UTF-8"), null);
    }
    
//    public static void main (String [] atg) {
//        URIResourceIDSections uri = new URIResourceIDSections();
//        uri.setAuthority("localhost");
//        uri.setScheme("http");
//        uri.setNamespace("la la la");
//        uri.setPattern("ddasdsa.*");
//        uri.setService("/sa da/dada");
//        
//        System.out.println(uri.toString());
//        
//        uri.setAuthority("localhost:23");
//        System.out.println(uri.toString());
//        
//    }
}
